{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download the Cognitive Diagnosis Benchmark Datasets (CDBD)\n",
    "from EduData import get_data\n",
    "\n",
    "get_data(\"math2015\", \"../../../data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'user_id': 0, 'item_id': 5, 'score': 1.0} {'user_id': 0, 'item_id': 8, 'score': 1.0}\n"
     ]
    }
   ],
   "source": [
    "# Data preprocessing, split train/valid/test data\n",
    "\n",
    "import numpy as np\n",
    "import random\n",
    "import json\n",
    "\n",
    "train_ratio = 0.8\n",
    "valid_ratio = 0\n",
    "# Q matrix\n",
    "np.savetxt(\"../../../data/math2015/Math1/q_m.csv\", np.loadtxt(\"../../../data/math2015/Math1/q.txt\", dtype=int), delimiter=',', fmt='%d')\n",
    "\n",
    "# response matrix, split dataset\n",
    "R = (np.loadtxt(\"../../../data/math2015/Math1/data.txt\") == 1).astype(float)\n",
    "stu_num, prob_num = R.shape[0], R.shape[1]\n",
    "train_logs, valid_logs, test_logs = [], [], []\n",
    "for stu in range(stu_num):\n",
    "    stu_logs = []\n",
    "    for prob in range(prob_num):\n",
    "        log = {'user_id': int(stu), 'item_id': int(prob), 'score': R[stu][prob]}\n",
    "        stu_logs.append(log)\n",
    "    random.shuffle(stu_logs)\n",
    "    train_logs += stu_logs[: int(train_ratio * prob_num)]\n",
    "    valid_logs += stu_logs[int(train_ratio * prob_num): int(train_ratio * prob_num) + int(valid_ratio * prob_num)]\n",
    "    test_logs += stu_logs[int(train_ratio * prob_num) + int(valid_ratio * prob_num):]\n",
    "\n",
    "with open(\"../../../data/math2015/Math1/train_data.json\", 'w', encoding='utf8') as file:\n",
    "    json.dump(train_logs, file, indent=4, ensure_ascii=False)\n",
    "with open(\"../../../data/math2015/Math1/valid_data.json\", 'w', encoding='utf8') as file:\n",
    "    json.dump(valid_logs, file, indent=4, ensure_ascii=False)\n",
    "with open(\"../../../data/math2015/Math1/test_data.json\", 'w', encoding='utf8') as file:\n",
    "    json.dump(test_logs, file, indent=4, ensure_ascii=False)\n",
    "\n",
    "print(train_logs[0], test_logs[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
